(*
let rec get_colors z = function                                
  | [] -> (0.,0.,0.)                                           
  | (alt,color)::l when alt = z -> color                       
  | e::l -> get_colors z l                                     
                                                               
let draw_face vertices get_color = function                    
  | (a,b,c) -> GlDraw.color (get_colors (c-1) Data.alt_color_list);
               GlDraw.vertex3 (vertices.(a-1));                
               GlDraw.vertex3 (vertices.(b-1));                
               GlDraw.vertex3 (vertices.(c-1))
  | _ -> failwith "face incomplete"
  
let draw_faces vertices l colors =                             
  Array.iter (draw_face vertices get_color)
 *)

(* GLGTK *)


let resizeGLScene ~width ~height =
  let ok_height = 
    if height = 0 then 1 else height in

  GlDraw.viewport 0 0 width ok_height;

  GlMat.mode `projection;
  GlMat.load_identity ();
  
  GluMat.perspective ~fovy:70.0 
    ~aspect:((float_of_int width)/.(float_of_int ok_height)) ~z:(0.1, 100.0);
    
  GlMat.mode `modelview;
  GlMat.load_identity ()



let initGL () =
  GlDraw.shade_model `smooth;
  
  GlClear.color ~alpha:0.0 (0.0, 0.0, 0.0);

  GlClear.depth 1.0;
  Gl.enable `depth_test;
  GlFunc.depth_func `lequal;

  GlMisc.hint `perspective_correction `nicest






let drawGLScene area () =
  GlClear.clear [`color; `depth];
  GlMat.load_identity ();
                      
  GlMat.rotate ~angle:(400. *. Sys.time ()) ~x:0. ~y:0. ~z:1. ();
  
  GlDraw.begins `triangles;

(*
  draw_faces [|(1., 0., 0.),(0., 0., 1.),(0., 1., 0.),(0., 0., 0.)|] 
    [(1,2,3),(1,3,4),(2,3,4),(2,1,4)] 
    [|(1.,0.,0.),(0.,1.,0.),(0.,0.,1.),(0.6,0.4,0.2)|];
 *)

  GlDraw.color (1.0, 0.0, 0.0);
  GlDraw.vertex3 (0.0, 1.0, 0.0);
  GlDraw.color (0.0, 1.0, 0.0);
  GlDraw.vertex3 (-1.0, -1.0, 1.0);
  GlDraw.color (0.0, 0.0, 1.0);
  GlDraw.vertex3 (1.0, -1.0, 1.0);

  GlDraw.color (1.0, 0.0, 0.0);
  GlDraw.vertex3 (0.0, 1.0, 0.0);
  GlDraw.color (0.0, 0.0, 1.0);
  GlDraw.vertex3 (1.0, -1.0, 1.0);
  GlDraw.color (0.0, 1.0, 0.0);
  GlDraw.vertex3 (1.0, -1.0, -1.0);

  GlDraw.color (1.0, 0.0, 0.0);
  GlDraw.vertex3 (0.0, 1.0, 0.0);
  GlDraw.color (0.0, 1.0, 0.0);
  GlDraw.vertex3 (1.0, -1.0, -1.0);
  GlDraw.color (0.0, 0.0, 1.0);
  GlDraw.vertex3 (-1.0, -1.0, -1.0);

  GlDraw.color (1.0, 0.0, 0.0);
  GlDraw.vertex3 (0.0, 1.0, 0.0);
  GlDraw.color (0.0, 0.0, 1.0);
  GlDraw.vertex3 (-1.0, -1.0, -1.0);
  GlDraw.color (0.0, 1.0, 0.0);
  GlDraw.vertex3 (-1.0, -1.0, 1.0);

  GlDraw.ends ();

  area#swap_buffers ()







let killGLWindow () =
  () (* do nothing *)

let createGLWindow title width height bits fullscreen =
  let w = GWindow.window ~title:title () in
  ignore(w#connect#destroy ~callback:(fun () -> GMain.Main.quit (); exit 0));
  w#set_resize_mode `IMMEDIATE;
  let area = GlGtk.area [`DOUBLEBUFFER;`RGBA;`DEPTH_SIZE 16;`BUFFER_SIZE bits]
      ~width:width ~height:height~packing:w#add () in
  area#event#add [`KEY_PRESS];

  ignore(w#event#connect#key_press ~callback:
    begin fun ev ->
      let key = GdkEvent.Key.keyval ev in
      if key = GdkKeysyms._Escape then w#destroy ();
      true
    end);

  ignore(GMain.Timeout.add ~ms:20 ~callback:
    begin fun () ->
      drawGLScene area (); true
    end);

  ignore(area#connect#display ~callback:(drawGLScene area));
  ignore(area#connect#reshape ~callback:resizeGLScene);

  ignore(area#connect#realize ~callback:
    begin fun () ->
      initGL ();
      resizeGLScene ~width ~height
    end);
  w#show ();

  w


let main () =
  let w = createGLWindow "Modélisation 3D" 640 480 16 false in
  GMain.Main.main ()

let _ = Printexc.print main ()
